package com.changpay.zywy.ui.activity.mine

import android.Manifest
import android.content.Intent
import android.text.TextUtils
import android.view.View
import com.changpay.zywy.R
import com.changpay.zywy.base.BaseActivity
import com.changpay.zywy.base.IView
import com.changpay.zywy.constant.Constant
import com.changpay.zywy.ext.showToast
import com.changpay.zywy.ext.sss
import com.changpay.zywy.help.ImageHelper
import com.changpay.zywy.help.PermissionHelper
import com.changpay.zywy.http.RetrofitHelper
import com.changpay.zywy.utils.DialogUtil
import com.changpay.zywy.utils.ImageLoader
import com.changpay.zywy.utils.Preference
import com.luck.picture.lib.PictureSelector
import com.luck.picture.lib.config.PictureConfig
import kotlinx.android.synthetic.main.activity_edit_info.*
import kotlinx.android.synthetic.main.title_right_layout.*
import java.util.HashMap


class EditUserInfoActivity : BaseActivity(), IView {

    private var head_image: String by Preference(Constant.HEAD_IMAGE, "")
    private var true_name: String by Preference(Constant.TRUE_NAME, "")
    private var sex: String by Preference(Constant.SEX, "")
    private var card_number: String by Preference(Constant.CARD_NUMBER, "")
    private var adress: String by Preference(Constant.ADRESS, "")
    private var city: String by Preference(Constant.CITY, "")
    private var age: String by Preference(Constant.AGE, "")

    private var compressPath: String = ""
    private var areaId = ""

    private val mDialog by lazy {
        DialogUtil.getWaitDialog(this, "修改中")
    }

    override fun showLoading() {
        mDialog.show()
    }

    override fun hideLoading() {
        mDialog.dismiss()
    }


    private val onClickListener = View.OnClickListener { view ->
        when (view.id) {
            R.id.change_avatar_rlt -> {
                PermissionHelper.instance.getPermission(this, object : PermissionHelper.PermissionCallBack {
                    override fun onSuccess() {
                        ImageHelper.showAvatarChoose(this@EditUserInfoActivity)
                    }

                    override fun onFail() {
                        showToast("请先允许权限再使用此功能")
                    }
                }, Manifest.permission.WRITE_EXTERNAL_STORAGE)
            }
            R.id.tv_area -> Intent(this, LocationSelectActivity::class.java).run {
                startActivityForResult(this, 100)
            }
            R.id.tv_right -> save()
            R.id.iv_back -> finish()
        }
    }

    override fun attachLayoutRes(): Int {
        return R.layout.activity_edit_info
    }

    override fun showDefaultMsg(msg: String) {
        showToast(msg)
    }

    override fun showMsg(msg: String) {
        // 通过此回调来保存图片路径
        submit(msg)
    }

    override fun showError(errorMsg: String) {
        showToast(errorMsg)
    }

    override fun initData() {
    }

    override fun initView() {
        tv_title.text = "个人信息"
        tv_right.text = "保存"
        iv_back.setOnClickListener(onClickListener)
        ImageLoader.loadAvatar(head_image, avatar_iv)
        et_true_name.setText(true_name)
        et_sex.setText(sex)
        et_age.setText(age)
        et_qr_card.setText(card_number)
        tv_area.text = city
        et_address.setText(adress)
        tv_area.setOnClickListener(onClickListener)
        tv_right.setOnClickListener(onClickListener)
        change_avatar_rlt.setOnClickListener(onClickListener)


    }

    override fun start() {}

    private fun save() {
        if (TextUtils.isEmpty(compressPath)) {
            submit(head_image)
        } else {
            ImageHelper.upLoadImage(this, token, compressPath)
        }
    }


    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (resultCode == RESULT_OK) {
            when (requestCode) {
                PictureConfig.CHOOSE_REQUEST -> {
                    val localMedias = PictureSelector.obtainMultipleResult(data)
                    compressPath = localMedias[0].compressPath
                    ImageLoader.loadAvatar(compressPath, avatar_iv)

                }
            }
        }
        if (resultCode == 200) {
            areaId = data?.getStringExtra("areaId").toString()
            tv_area.text = data?.getStringExtra("area_name").toString()
        }
    }


    private fun submit(member_avatar: String) {
        if (TextUtils.isEmpty( et_address.text.toString())) {
            showToast("联系地址不能为空")
            return
        }
        val params = HashMap<String, String>()
        params["key"] = token
        params["member_name"] = et_true_name.text.toString()
        params["member_avatar"] = member_avatar
        params["evolution_city_id"] = areaId
        params["evolution_city"] = tv_area.text.toString()
        params["evolution_address"] = et_address.text.toString()
        RetrofitHelper.service.edit_member_info(params).sss(this)
        {
            city = tv_area.text.toString()
            head_image = member_avatar
            adress =  et_address.text.toString()
            showToast("修改成功")
            finish()
        }
    }
}
